package me.linczh.bank.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import me.linczh.bank.bean.AccountBean;
import me.linczh.bank.bean.enums.AccountType;
import me.linczh.bank.bean.enums.DepositType;
import me.linczh.bank.dao.AccountDao;
import me.linczh.framework.persiste.IdGenerator;
import me.linczh.framework.query.Query;

public class DBAccountDao extends BaseDBDaoImpl implements AccountDao {

	@Override
	public AccountBean create(AccountBean _account) {
		// TODO Auto-generated method stub
		_account.setCreatetime(new Date());
		String nextId = IdGenerator.nextId(_account);
		if (nextId != null) {
			_account.setAccount(nextId);
			try {
				SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String time = fmt.format(_account.getCreatetime());
				Statement s = super.conn.createStatement();
				s.execute("insert into account_tbl values (" +
						"'" + _account.getAccount() + "'," +
						"'" + _account.getUsername() + "'," +
						"'" + _account.getPassword() + "'," +
						"'" + _account.getIdentity() + "'," +
						_account.getBalance() + "," +
						(_account.getFrozen() ? 1 : 0) + "," +
						"'" + _account.getAccounttype() + "'," +
						"'" + _account.getDeposittype() + "'," +
						"'" + time + "')");
				return _account;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public boolean update(AccountBean _account) {
		// TODO Auto-generated method stub
		try {
			Statement s = super.conn.createStatement();
			int succ = s.executeUpdate("update account_tbl set " +
					"password='" + _account.getPassword() + "'," +
					"balance=" + _account.getBalance() + "," +
					"frozen=" + (_account.getFrozen()?1:0) + " where " +
					"account='" + _account.getAccount() + "'");
			if (succ == 1) return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public AccountBean findById(String accountID) {
		// TODO Auto-generated method stub
		try {
			Statement s = super.conn.createStatement();
			ResultSet result = s.executeQuery("select * from account_tbl where " +
					"account='" + accountID + "'");
			if (result.next()) {
				SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				AccountBean bean = new AccountBean();
				bean.setAccount(result.getString(1));
				bean.setUsername(result.getString(2));
				bean.setPassword(result.getString(3));
				bean.setIdentity(result.getString(4));
				bean.setBalance(result.getDouble(5));
				bean.setFrozen(result.getInt(6) == 1 ? true : false);
				bean.setAccounttype(AccountType.valueOf(result.getString(7)));
				bean.setDeposittype(DepositType.valueOf(result.getString(8)));
				bean.setCreatetime(fmt.parse(result.getString(9)));
				return bean;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<AccountBean> query(Query<AccountBean> query) {
		// TODO Auto-generated method stub
		try {
			Statement s = super.conn.createStatement();
			ResultSet result = s.executeQuery(query.statement());
			List<AccountBean> list = new ArrayList<AccountBean>();
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			while (result.next()) {
				AccountBean bean = new AccountBean();
				bean.setAccount(result.getString(1));
				bean.setUsername(result.getString(2));
				bean.setPassword(result.getString(3));
				bean.setIdentity(result.getString(4));
				bean.setBalance(result.getDouble(5));
				bean.setFrozen(result.getInt(6) == 1 ? true : false);
				bean.setAccounttype(AccountType.valueOf(result.getString(7)));
				bean.setDeposittype(DepositType.valueOf(result.getString(8)));
				bean.setCreatetime(fmt.parse(result.getString(9)));
				list.add(bean);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ArrayList<AccountBean>();
	}

	@Override
	public boolean delete(AccountBean _account) {
		// TODO Auto-generated method stub
		try {
			Statement s = super.conn.createStatement();
			int succ = s.executeUpdate("delete from account_tbl where " +
					"account='" + _account.getAccount() +"'");
			if (succ > 0) return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

}
